import networkx as nx
import os


def Get_graph(data_name):
    """
    :param data_name: 数据集名字
    :return: 无向图网络
    """
    current_dir = os.path.dirname(os.path.abspath(__file__))
    G = nx.Graph()  # 创建无向图
    file_path = current_dir + '\\data\\' + data_name + '.txt'  # 注意替换为你的实际文件名
    with open(file_path, 'r') as f:
        for line in f:
            parts = line.strip().split()
            if len(parts) != 2:
                continue  # 跳过异常行
            u, v = map(int, parts)
            G.add_edge(u, v)  # 加边

    print('\n', f"当前数据集有{G.number_of_nodes()}个节点，{G.number_of_edges()}条边。")
    return G

def Get_Kcore(G):
    """
    :param G: 无向图网络
    :return: 每个节点的K-core值字典
    """
    core_dict = nx.core_number(G)  # 返回 {node: core_number}
    return core_dict